package io.renren.modules.student.controller;

import io.renren.common.utils.R;
import io.renren.common.utils.UploadUtil;
import io.renren.modules.student.service.StuPersonInfoService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/stu/personInfo")
public class StuPersonInfoController extends AbstractController {
    @Autowired
    private StuPersonInfoService stuPersonInfoService;
    /*
    获得基本信息
     */
    @GetMapping("/baseInfo")
    public R queryBaseInfo(){
        Map<String, Object> result = stuPersonInfoService.queryBaseInfo(getUserId()+"");
        return R.ok().put("data",result);
    }

    /*
    更新基本信息数据
     */
    @PostMapping("/updateInfo")
  public R update(@RequestBody Map<String, Object> formData){
        formData.put("userId",getUserId()+"");
         stuPersonInfoService.updateBaseInfo(formData);
        return R.ok();
    }
    /*
   更换头像
    */
    @PostMapping("/uploadIcon")
    public R upload(@RequestParam(value = "file") MultipartFile file){
        try{
            String savePath = UploadUtil.saveFile(file,"Icon"+"/teacherIcon/"+getUserId(),UploadUtil.FileType.IMG_FILE);
            return R.ok().put("filePath",savePath);
        }catch (IOException e){
            return R.error(e.getMessage());
        }

    }

    /*
    存头像
     */
    @PostMapping("/saveIcon")
    public R saveIcon(@RequestBody Map<String, Object> iconUrl){
        iconUrl.put("userId",getUserId()+"");
        stuPersonInfoService.saveIcon(iconUrl);
        return R.ok();
    }
}
